package com.shh.rabbitmq.ttl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TtlRabbitMqConfiguration {

	 //1.声明注册direct模式的交换机
    @Bean
    public DirectExchange ttlExchange(){
        return new DirectExchange("ttl_order_exchange",true,false);
    }
    //2.声明过期队列
    @Bean
    public Queue  ttlsmsQueue(){
    	//设置过期时间
        Map<String,Object> args = new HashMap<>();
    	args.put("x-message-ttl",5000);//这里一定是int类型
    	  args.put("x-dead-letter-exchange","dead_direct_exchange");
          args.put("x-dead-letter-routing-key","dead");//fanout不需要配置
        return new Queue("sms.ttl.queue",true,false,false,args);
    }
    //3.完成绑定关系
    @Bean
    public Binding  ttlsmsBingding(){
        return BindingBuilder.bind(ttlsmsQueue()).to(ttlExchange()).with("sms");
    }
}
